package cn.com.homeyoda.aimate.aihealth.ui.detail;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.ViewModelProviders;

import com.wzq.mvvmsmart.utils.ToastUtils;

import java.nio.charset.StandardCharsets;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.PopularDetailBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityDetailBinding;
import cn.com.homeyoda.aimate.aihealth.ui.detail.detail.DetailViewModel;

public class DetailActivity extends BaseActivity<ActivityDetailBinding, DetailViewModel> implements View.OnClickListener {
    String id = "";
    @Override
    public DetailViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(DetailViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        id = getIntent().getStringExtra("id");
        binding.titleBar.ivBack.setOnClickListener(this);
        binding.btnVoice.setOnClickListener(this);
        binding.titleBar.tvTitle.setText(getString(R.string.daily_kepu));
        viewModel.getPopularDetail(id);
        //initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setData(PopularDetailBean.DataBean dataBean) {
        binding.tvTitle.setText(dataBean.getTitle());
        binding.tvAuthor.setText("发布人："+dataBean.getReleasePeopleId());
        binding.tvTime.setText("发布时间："+dataBean.getReleaseTime());
        binding.tvRead.setText("阅读："+dataBean.getBrowseCount());
        if(dataBean.getSoundUrl() == null){
            binding.btnVoice.setVisibility(View.GONE);
        }
        String content = decodeBase64ToString(dataBean.getDetail());
        binding.wbContent.getSettings().setJavaScriptEnabled(true);
        binding.wbContent.getSettings().setDomStorageEnabled(true);
        binding.wbContent.getSettings().setLoadsImagesAutomatically(true);
        binding.wbContent.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        binding.wbContent.setLayerType(View.LAYER_TYPE_HARDWARE, null); // 优先尝试硬件加速
        binding.wbContent.setBackgroundColor(Color.TRANSPARENT);
//        binding.wbContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        binding.wbContent.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.stateLiveData.observe(this, dataBean -> {
            setData(dataBean);
        });
    }

    public String decodeBase64ToString(String base64String) {
        byte[] decodedBytes = Base64.decode(base64String, Base64.DEFAULT);
        return new String(decodedBytes, StandardCharsets.UTF_8);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.btn_voice) {
            ToastUtils.showShort("语音播报");
        }
    }

}